package com.chenhao.spingcloud.chenhaoeurekauser.controller;

import com.chenhao.spingcloud.chenhaoeurekauser.model.Product;
import com.chenhao.spingcloud.chenhaoeurekauser.pojo.Result;
import com.chenhao.spingcloud.chenhaoeurekauser.pojo.User;
import com.chenhao.spingcloud.chenhaoeurekauser.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 *
 * @return
 * @author chenhao
 *
 * @date 2020/4/9 17:43
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;



    @GetMapping("/{id}")
    public Result <User> getUser(@PathVariable Long id) {
        //User user = userService.getUser(id);
        LOGGER.info("根据id获取用户信息，用户名称为：{}");
        User user = new User();
        user.setId(1L);
        user.setPassword("gggg");
        return new Result<User>(user);
    }

    /**
     * 查询全部
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/listAll")
    public Object listAll(@RequestParam(value = "page",defaultValue = "1")int page,
                          @RequestParam(value = "size",defaultValue = "10")int size){
        return userService.listAll(page, size);
    }

    /**
     * 添加数据
     * @param user
     * @return
     */
    @RequestMapping("/insert")
    public int insert (Product user){
        return userService.insert(user);
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @RequestMapping("/remove")
    public int remove(Integer userId){
        return userService.remove(userId);
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public int update(Product user){
        return userService.update(user);
    }

    /**
     * 乐观锁
     * @param id
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author chenhao
     *
     * @date 2020/4/14 13:50
     */
    @RequestMapping(value = "optimisticLock",method = RequestMethod.GET)
    public Map <String,String> optimisticLock(int userid, int id){
        Map<String,String> dataMap = userService.optimisticLock(userid,id);
        return dataMap;
    }


    /**
     *  悲观锁
     * @param userid
     * @param id
     * @param model
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author chenhao
     *
     * @date 2020/4/14 13:50
     */
    @RequestMapping(value = "pessimismLock",method = RequestMethod.GET)
    public Map<String,String> pessimismLock(int userid,int id){
        Map<String,String> dataMap = userService.pessimismLock(userid,id);
        return dataMap;
    }


}
